package com.zzyl.web.controller.nursing;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.dto.DeviceDto;
import com.zzyl.nursing.domain.pojo.Device;
import com.zzyl.nursing.domain.vo.DeviceDetailVo;
import com.zzyl.nursing.service.IDeviceService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备Controller
 * @author alexis
 * @date 2025-05-28
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/nursing/device")
@RequiredArgsConstructor
public class DeviceController extends BaseController {
    private final IDeviceService deviceService;
    
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }
    
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        return success(deviceService.allProduct());
    }
    
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation(value = "查询产品详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "productKey", value = "产品id", required = true, dataTypeClass = String.class)})
    public AjaxResult queryProduct(@PathVariable String productKey) {
        return deviceService.queryProduct(productKey);
    }
    
    /**
     * 查询设备列表
     */
    @ApiOperation("查询设备列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo<List<Device>> list(@ApiParam("设备查询条件") Device device) {
        startPage();
        List<Device> list=deviceService.selectDeviceList(device);
        return getDataTable(list);
    }
    
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.registerDevice(deviceDto);
        return success();
    }
    
    /**
     * 获取设备详细信息
     */
    @ApiOperation("获取设备详细信息")
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> getInfo(@PathVariable String iotId) {
        return R.ok(deviceService.selectDeviceBy(iotId));
    }
    
    @ApiOperation("获取设备运行数据")
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@PathVariable String iotId) {
        return deviceService.queryServiceProperties(iotId);
    }
    
    /**
     * 新增设备
     */
    @ApiOperation("新增设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("设备信息") @RequestBody Device device) {
        return toAjax(deviceService.insertDevice(device));
    }
    
    /**
     * 修改设备
     */
    @ApiOperation("修改设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("设备信息") @RequestBody Device device) {
        return toAjax(deviceService.updateDevice(device));
    }
    
    /**
     * 删除设备
     */
    @ApiOperation("删除设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("设备ID数组") @PathVariable Long[] ids) {
        return toAjax(deviceService.deleteDeviceByIds(ids));
    }
}
